﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;

namespace polyvDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string MD5Encrypt(string str)
            {

                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] bytValue, bytHash;
                bytValue = Encoding.UTF8.GetBytes(str);
                bytHash = md5.ComputeHash(bytValue);
                md5.Clear();
                string sTemp = "";
                for (int i = 0; i < bytHash.Length; i++)
                {
                    sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
                }
                return sTemp;

            }
            string GetTimeStamp()
            {
                TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                UInt64 ss = Convert.ToUInt64(ts.TotalMilliseconds);
                return ss.ToString();
            }

            Console.WriteLine("Hello World!");

            using (var client = new HttpClient())
            {
                using (var multipartFormDataContent = new MultipartFormDataContent())
                {

                    string filePath = "D:\\Files\\excel\\20190816\\661a2126-b601-435f-a217-6452be487015.xls";
                    string appId = "fct7mofxfo";
                    string channelId = "365800";
                    string timestamp = GetTimeStamp();
                    string appSecret = "157ebe5514dc461690847a7643e98b65";
                    int rank = 1;
                    string concated = string.Format("appId{0}channelId{1}rank{2}timestamp{3}", appId, channelId, rank, timestamp);
                    string sign = MD5Encrypt(appSecret + concated + appSecret).ToUpper();
                    string content = string.Format("appId={0}&channelId={1}&timestamp={2}&sign={3}&rank={4}", appId, channelId, timestamp, sign, rank);

                    var values = new[]
                    {
            new KeyValuePair<string, string>("appId", appId),
            new KeyValuePair<string, string>("channelId", channelId),
            new KeyValuePair<string, string>("rank", rank.ToString()),
            new KeyValuePair<string, string>("sign", sign),
            new KeyValuePair<string, string>("timestamp", timestamp)

             //other values
        };

                    foreach (var keyValuePair in values)
                    {
                        multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
                            String.Format("\"{0}\"", keyValuePair.Key));
                    }

                    multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)),
                        "\"file\"",
                        "\"ce.xls\"");

                    var requestUri = "http://api.polyv.net/live/v3/channel/auth/upload-white-list";
                    var html = client.PostAsync(requestUri, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
                    Console.WriteLine("Hello World!");
                }
            }


        }
    }
}
